#include <QRandomGenerator>
#include <QPainter>

QImage generateImageCode(QString &code, int width, int height) {

    const QString charPool = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    int codeLength = 4;

    code.clear();
    for (int i = 0; i < codeLength; ++i) {
        int index = QRandomGenerator::global()->bounded(charPool.size());
        code.append(charPool[index]);
    }

    QImage image(width, height, QImage::Format_ARGB32);
    image.fill(Qt::white);

    QPainter painter(&image);
    painter.setRenderHint(QPainter::Antialiasing);
    QFont font("Arial", height / 2);
    painter.setFont(font);

    QRect rect(0, 0, width, height);
    painter.setPen(Qt::black);
    painter.drawText(rect, Qt::AlignCenter, code);

    for (int i = 0; i < 5; ++i) {
        QPen pen(QColor::fromRgb(QRandomGenerator::global()->bounded(256),
                                 QRandomGenerator::global()->bounded(256),
                                 QRandomGenerator::global()->bounded(256)));
        pen.setWidth(2);
        painter.setPen(pen);
        int x1 = QRandomGenerator::global()->bounded(width);
        int y1 = QRandomGenerator::global()->bounded(height);
        int x2 = QRandomGenerator::global()->bounded(width);
        int y2 = QRandomGenerator::global()->bounded(height);
        painter.drawLine(x1, y1, x2, y2);
    }

    return image;
}
